package z_map;

import java.awt.EventQueue;

import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JPanel;

import java.awt.Color;
import java.awt.SystemColor;
import java.util.HashMap;

import javax.swing.SwingConstants;
import javax.swing.JSeparator;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class MapUpdate extends JInternalFrame {
	
	HashMap<String, MapData> map = new HashMap();
	boolean findFlag = false;
	
	private JLabel lblNewLabel;
	private JTextField findID;
	private JButton btnNewButton;
	private JPanel panel;
	private JLabel lblNewLabel_1;
	private JTextField irum;
	private JLabel lblNewLabel_2;
	private JTextField phone;
	private JButton btnNewButton_1;
	private JButton btnNewButton_2;
	private JLabel status;


	/**
	 * Create the frame.
	 */
	public MapUpdate(HashMap map) {
		super("Update",true, true, true, false);
		this.map = map;
		setBounds(100, 100, 390, 270);
		getContentPane().setLayout(null);
		getContentPane().add(getPanel());
		getContentPane().add(getLblNewLabel_1());
		getContentPane().add(getIrum());
		getContentPane().add(getLblNewLabel_2());
		getContentPane().add(getPhone());
		getContentPane().add(getBtnNewButton_1());
		getContentPane().add(getBtnNewButton_2());
		getContentPane().add(getStatus());

	}
	private JLabel getLblNewLabel() {
		if (lblNewLabel == null) {
			lblNewLabel = new JLabel("아이디");
			lblNewLabel.setBounds(12, 14, 57, 15);
		}
		return lblNewLabel;
	}
	private JTextField getFindID() {
		if (findID == null) {
			findID = new JTextField();
			findID.setBounds(63, 11, 163, 21);
			findID.setColumns(10);
		}
		return findID;
	}
	private JButton getBtnNewButton() {
		if (btnNewButton == null) {
			btnNewButton = new JButton("조회");
			btnNewButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					String find = findID.getText();
					findFlag = false;
					if(find.equals("")){
						status.setText("검색어할 아이디를 입력하세요.\n");
					}else{
						MapData d = map.get(find);
						if(d==null){
							status.setText("찾는 데이터가 없습니다.\n");
							irum.setText("");
							phone.setText("");
						}else{
							findFlag=true;
							irum.setText(d.getIrum());
							phone.setText(d.getPhone());
							
							status.setText("검색이 완료되었습니다.\n");
						}
					}
				}
			});
			btnNewButton.setBounds(238, 10, 97, 23);
		}
		return btnNewButton;
	}
	private JPanel getPanel() {
		if (panel == null) {
			panel = new JPanel();
			panel.setBackground(SystemColor.inactiveCaption);
			panel.setBounds(12, 10, 347, 45);
			panel.setLayout(null);
			panel.add(getLblNewLabel());
			panel.add(getFindID());
			panel.add(getBtnNewButton());
		}
		return panel;
	}
	private JLabel getLblNewLabel_1() {
		if (lblNewLabel_1 == null) {
			lblNewLabel_1 = new JLabel("성명");
			lblNewLabel_1.setHorizontalAlignment(SwingConstants.RIGHT);
			lblNewLabel_1.setBounds(12, 79, 57, 15);
		}
		return lblNewLabel_1;
	}
	private JTextField getIrum() {
		if (irum == null) {
			irum = new JTextField();
			irum.setBounds(91, 76, 116, 21);
			irum.setColumns(10);
		}
		return irum;
	}
	private JLabel getLblNewLabel_2() {
		if (lblNewLabel_2 == null) {
			lblNewLabel_2 = new JLabel("연락처");
			lblNewLabel_2.setHorizontalAlignment(SwingConstants.RIGHT);
			lblNewLabel_2.setBounds(12, 116, 57, 15);
		}
		return lblNewLabel_2;
	}
	private JTextField getPhone() {
		if (phone == null) {
			phone = new JTextField();
			phone.setBounds(91, 113, 183, 21);
			phone.setColumns(10);
		}
		return phone;
	}
	private JButton getBtnNewButton_1() {
		if (btnNewButton_1 == null) {
			btnNewButton_1 = new JButton("수정");
			btnNewButton_1.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					if(findFlag){
						int b = JOptionPane.showConfirmDialog(null, "정말 수정 ???");
						if(b == JOptionPane.YES_OPTION){
							String id = findID.getText();
							String ir = irum.getText();
							String ph = phone.getText();
							
							MapData d = new MapData(id, ir, ph);
							map.put(id, d);
							
							status.setText(id + " (이)가 수정 되었습니다.");
						}else{
							status.setText("수정이 취소 되었습니다.");
						}
						
					}else{
						status.setText("데이터가 검색되지 않았습니다.");
					}
				}
			});
			btnNewButton_1.setBounds(91, 160, 97, 23);
		}
		return btnNewButton_1;
	}
	private JButton getBtnNewButton_2() {
		if (btnNewButton_2 == null) {
			btnNewButton_2 = new JButton("삭제");
			btnNewButton_2.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					if(findFlag){
						int b = JOptionPane.showConfirmDialog(null, "정말 삭제 ???");
						if(b == JOptionPane.YES_OPTION){
							String id = findID.getText();
							map.remove(id);
							status.setText(id + " (이)가 삭제 되었습니다.");
						}else{
							status.setText("삭제가 취소 되었습니다.");
						}
						
					}else{
						status.setText("데이터가 검색되지 않았습니다.");
					}
				}
			});
			btnNewButton_2.setBounds(200, 160, 97, 23);
		}
		return btnNewButton_2;
	}
	private JLabel getStatus() {
		if (status == null) {
			status = new JLabel("New label");
			status.setBackground(SystemColor.activeCaptionBorder);
			status.setOpaque(true);
			status.setBounds(12, 202, 347, 21);
		}
		return status;
	}
}
